package org.whuims.web.news;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;
import org.whuims.entity.Infor;
import org.whuims.entity.User;
import org.whuims.service.account.ShiroDbRealm;
import org.whuims.service.comment.CommentService;
import org.whuims.service.news.InforService;

import javax.servlet.ServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Qikai on 2016/12/12.
 */
@Controller
@RequestMapping(value = "/infor")
public class InforController {
    private static final String PAGE_SIZE = "10";
    @Autowired
    private InforService inforService;
    @Autowired
    private CommentService commentService;

    @RequestMapping(value = "list/{type}", method = RequestMethod.GET)
    public String listInforsByType(@PathVariable("type") Integer type, @RequestParam(value = "page", defaultValue = "1") int pageNumber,
                                   @RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
                                   @RequestParam(value = "sortType", defaultValue = "auto") String sortType, Model model,
                                   ServletRequest request) {
        Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
        Page<Infor> inforPage = this.inforService.getTypeInfors(type, searchParams, pageNumber, pageSize, sortType);
        model.addAttribute("inforPage", inforPage);
        return "infor/inforlist";
    }

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(ServletRequest request, Model model) {
        Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
        Page<Infor> inforPage1 = this.inforService.getTypeInfors(1, searchParams, 1, 5, "auto");
        Page<Infor> inforPage2 = this.inforService.getTypeInfors(2, searchParams, 1, 5, "auto");
        Page<Infor> inforPage3 = this.inforService.getTypeInfors(3, searchParams, 1, 5, "auto");
        Page<Infor> inforPage4 = this.inforService.getTypeInfors(4, searchParams, 1, 5, "auto");
        model.addAttribute("inforPage1", inforPage1);
        model.addAttribute("inforPage2", inforPage2);
        model.addAttribute("inforPage3", inforPage3);
        model.addAttribute("inforPage4", inforPage4);
        return "infor/infors";
    }

    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String createForm(Model model) {
        Infor infor = new Infor();
        infor.setPublishDate(new Date());
        model.addAttribute("infor", infor);
        model.addAttribute("action", "create");
        return "infor/inforedit";
    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public String create(Infor infor, RedirectAttributes redirectAttributes) {
        System.out.println("save infor entity successfully");
        User user = new User(getCurrentUserId());
        infor.setAuthor(user);
        this.inforService.saveInfor(infor);
        redirectAttributes.addFlashAttribute("message", "创建任务成功");
        return "redirect:/infor/detail/" + infor.getId();
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(@Valid @ModelAttribute("infor") Infor infor, RedirectAttributes redirectAttributes) {
        User user = new User(getCurrentUserId());
        infor.setAuthor(user);
        inforService.saveInfor(infor);
        redirectAttributes.addFlashAttribute("message", "更新成功");
        return "redirect:/news/detail/" + infor.getId();
    }


    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String updateForm(@PathVariable("id") Long id, Model model) {
        Infor infor = this.inforService.getInfor(id);
        model.addAttribute("action", "update");
        model.addAttribute(infor);
        return "infor/inforedit";
    }

    @RequestMapping(value = "detail/{id}", method = RequestMethod.GET)
    public String detail(@PathVariable("id") Long id,
                         @RequestParam(required = false, defaultValue = "1") Integer page,
                         @RequestParam(required = false, defaultValue = "10") Integer size,
                         Model model) {
        Infor infor = this.inforService.getInfor(id);
        model.addAttribute("infor", infor);
        model.addAttribute("commentPage", commentService.pageByInfor(infor, page, size));
        List<Infor> infors = this.inforService.findTopInfors(8);
        System.out.println(infors.size());
        model.addAttribute("infors", infors);
        return "infor/infordetail";
    }

    /**
     * 取出Shiro中的当前用户Id.
     */
    private Long getCurrentUserId() {
        ShiroDbRealm.ShiroUser user = (ShiroDbRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        return user.id;
    }
}
